package com.example.hanyh1.storedata;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.demo.api.TestApiManager;

import java.lang.ref.WeakReference;


/**
 * android gradle playGround
 * <p>
 * date:2018.10.29 forever
 *
 *Javassist -> .class文件修改器
 * Bugly ->腾讯日志监控
 *
 * @link https://www.cnblogs.com/zhaohongtian/p/6808962.html
 * @link https://www.jianshu.com/p/ed4ef3b96a29
 * /////////////以上是大纲,以下TODO是将要做的///////////////////////////////
 * @link http://blog.zhaiyifan.cn/2016/02/18/android-resource-overlay/      重迭包
 *
 * @link https://www.jianshu.com/p/873eaee38cc1      自定义插件
 * @link https://blog.csdn.net/llew2011/article/details/78540911
 *
 * @link http://blog.zhaiyifan.cn/2016/02/18/android-resource-overlay/     编译时候替换资源
 * @link https://www.jianshu.com/p/fef382dbce72
 *
 *
 * 1、演示在activity和application下面的meta_data标签的占位符使用当然在service和reciver中的用法
 * 2、演示gradle.properties中的自定义属性在buildConfig和resValue中的使用
 * 3、演示ext设置全局属性、比如compileVersion、targetVersion等公共属性可以放入自定义全局属性里面
 *    具体参考@link https://blog.csdn.net/struggle1103/article/details/80379961
 * 4、演示在项目中layout的分类具体参考@link https://blog.csdn.net/u011156012/article/details/50575117
 * 5、演示在项目中不同的productFlavors的场景可以替换manifestPlaceholders、buildConfigField、resValue等
 *    具体参考@link https://blog.csdn.net/zz6880817/article/details/79449465
 *
 * 6、如何提升gradle的编译速度 gradle.properties中添加参数、然后屏蔽一些task,具体方法@link https://www.jianshu.com/p/0d67f68beb38
 * 7、阿里的急速变异框架 @link https://github.com/alibaba/freeline
 *
 * * 可以参照上面的link文章
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button btnActivityMeta, btnApplicationMeta, btnBuidconfig, btnResValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        addLiseners();

    }

    private void addLiseners() {
        btnActivityMeta.setOnClickListener(this);
        btnApplicationMeta.setOnClickListener(this);
        btnBuidconfig.setOnClickListener(this);
        btnResValue.setOnClickListener(this);
    }

    private void initView() {
        btnActivityMeta = (Button) findViewById(R.id.btn_activity_meta);
        btnApplicationMeta = (Button) findViewById(R.id.btn_application_meta);
        btnBuidconfig = (Button) findViewById(R.id.btn_buid_config);
        btnResValue = (Button) findViewById(R.id.btn_res_value);
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.btn_activity_meta:
                getActivityMetaData();
                break;

            case R.id.btn_application_meta:
                getApplicationMetaData();
                break;

            case R.id.btn_buid_config:
                getBuildConfigValue();
                break;

            case R.id.btn_res_value:
                getResValue();
                break;
        }
    }

    private void getActivityMetaData() {
        ActivityInfo info = TestApiManager.getInstance().getActivityInfo(new WeakReference<Activity>(this));
        if (info != null) {

            String nameActivity = info.metaData.getString("nameActivity");
            Toast.makeText(this, nameActivity, Toast.LENGTH_LONG).show();
        }
    }

    private void getApplicationMetaData() {

        ApplicationInfo info = TestApiManager.getInstance().getApplicationInfo(new WeakReference<Context>(this));

        if (info != null) {
            String sInfo = info.metaData.getString("nameApplication");
            Toast.makeText(this, sInfo, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "application info is null ", Toast.LENGTH_LONG).show();
        }
    }

    private void getBuildConfigValue() {
        Toast.makeText(this,BuildConfig.AppName1,Toast.LENGTH_LONG).show();

    }

    private void getResValue() {
        //用context的getResource方法获取自定义资源resValue的值
        String pw=getResources().getString(R.string.password);
        Toast.makeText(this,pw,Toast.LENGTH_LONG).show();
    }
}
